import 'package:flutter/material.dart';
import 'package:flutter_deer/database/beans/discount/discount_page_item_entity.dart';
import 'package:flutter_deer/generated/assets.dart';
import 'package:flutter_deer/res/colors.dart';
import 'package:flutter_deer/ui/cashier/provider/cashier_page_provider.dart';
import 'package:flutter_deer/widgets/cart_item_quantity_picker.dart';
import 'package:flutter_deer/widgets/dashed_border_text.dart';
import 'package:flutter_deer/widgets/horizontal_dashedLine.dart';
import 'package:get/get.dart';


class CouponCard extends StatelessWidget {
  CouponCard({
    super.key,
    required this.data,
    this.onTap, // 添加点击回调参数
  });

  final CouponBean data;
  final VoidCallback? onTap; // 定义点击回调
  final CashierPageProvider mProvider = Get.find<CashierPageProvider>();

  @override
  Widget build(BuildContext context) {
    // 定义尺寸常量
    final double dp9 = 9;
    final double dp6 = 6;
    final double dp1 = 1;
    final double dp15 = 15;
    final double dp4 = 4;
    final double dp67 = 67;
    final double dp20 = 20;
    final double dp5 = 5;
    const double dp3 = 3;
    final double dp2 = 2;
    final double dp16 = 16;
    final double dp12 = 12;
    final double dp30 = 30;
    final double dp24 = 24;
    final double dp13 = 13;
    final double dp18 = 18;
    final double dp8 = 8;
    final double dp38 = 38;
    final double dp10 = 10;

    return Padding(
      padding: EdgeInsets.fromLTRB(0, 0, 10, 0),
      child: Column(
        // mainAxisSize: MainAxisSize.min,
        children: [
          // 使用 InkWell 包裹主内容区域以添加点击事件
          InkWell(
            onTap: () {
              // 处理点击事件
              print('点击了优惠券---> ${data}');

              // if (data.available) {
              // }

              // 反选
              if (mProvider.selectedCoupon.contains(data)) {
                mProvider.selectedCoupon.remove(data);
                // mProvider.canStillOverlay(data);
                print('remove--->  ${data}');
                onTap?.call();
                return;
              }

              // 选择
              if (mProvider.canStillOverlay(data, tag: 'canStill--->')) {
                data.couponUseNum = 1;
                print('[---->] $data 优惠券添加');
                mProvider.selectedCoupon.add(data);
                onTap?.call();
                return;
              }

              mProvider.selectedCoupon.clear();
              data.couponUseNum = 1;
              mProvider.selectedCoupon.add(data);
              onTap?.call();

            }, // 设置点击回调
            child: Container(
              decoration: BoxDecoration(
                color: Colors.white,
                borderRadius: BorderRadius.circular(8),
                border: const Border(
                  top: BorderSide(color: Colours.c_F6E2C8, width: 0),
                  left: BorderSide(color: Colours.c_F6E2C8, width: 0),
                  right: BorderSide(color: Colours.c_F6E2C8, width: 0),
                  bottom: BorderSide(color: Colours.c_F6E2C8, width: 0),
                ),
              ),
              padding: EdgeInsets.zero,
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  // 第一行图标
                  SizedBox(
                      height: 30, // 根据
                      child: Stack(
                        fit: StackFit.passthrough, // 根据实际需求调整宽度
                        children: [
                          // 可用数量标签
                          if (data.isOverlay)
                            Positioned(
                                top: 0,
                                left: 0,
                                child: Container(
                                  padding: const EdgeInsets.fromLTRB(10, 3, 10, 3),
                                  decoration: const BoxDecoration(
                                    color: Color(0xFFFF5142),
                                    borderRadius: BorderRadius.only(
                                      topLeft: Radius.circular(8),
                                      bottomRight: Radius.circular(8),
                                    ),
                                  ),
                                  child: Text(
                                    '共${data.couponNum}张券，可用${data.overlayLimit}张',
                                    style: const TextStyle(
                                      color: Colors.white,
                                      fontSize: 12,
                                    ),
                                  ),
                                )),

                          // 可叠加
                          Obx(() => Positioned(
                              top: 0,
                              right: 0,
                              child: Container(
                                width: mProvider.canStillOverlay(data) ? null : 0,
                                padding: const EdgeInsets.fromLTRB(10, 3, 10, 3),
                                decoration: const BoxDecoration(
                                  color: Color(0xFFFF5142),
                                  borderRadius: BorderRadius.only(
                                    bottomLeft: Radius.circular(8),
                                    topRight: Radius.circular(8),
                                  ),
                                ),
                                child: const Text(
                                  '可叠加',
                                  style: TextStyle(
                                    color: Colors.white,
                                    fontSize: 12,
                                  ),
                                ),
                              ))),

                          // 已选中
                          Obx(() => Positioned(
                              top: 0,
                              right: 0,
                              child: Image.asset(
                                Assets.orderPngCouponSelect,
                                width: mProvider.selectedCoupon.contains(data) ? 30 : 0,
                                height: 30,
                              ))),

                        ],
                      )),

                  // 顶部信息行
                  Padding(
                    padding: const EdgeInsets.only(left: 15, top: 0, bottom: 10, right: 15),
                    child: Row(
                      // 主轴对齐方式：让左侧一组和右侧文本两端对齐
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        // 优惠券名称
                        Flexible(
                          child: Row(
                            children: [
                              // 左侧三个文本组合
                              Flexible(
                                  // 限制左侧组合最大宽度，为第四个文本预留空间
                                  child: Row(
                                // mainAxisSize: MainAxisSize.min,
                                children: [
                                  Flexible(
                                    child: Text(
                                      data.couponName ?? '',
                                      maxLines: 1,
                                      overflow: TextOverflow.ellipsis,
                                      style: const TextStyle(
                                        color: Color(0xFF333333),
                                        fontSize: 18,
                                        fontWeight: FontWeight.bold,
                                      ),
                                    ),
                                  ),
                                  if ((data.remainDays ?? 100) <= 7)
                                    Padding(
                                      padding: EdgeInsets.only(left: dp4),
                                      child: const DashedBorderText(
                                        text: '即将到期',
                                      ),
                                    ),
                                  if (data.promotionsMode == 4)
                                    Container(
                                      margin: EdgeInsets.only(left: dp5),
                                      padding: EdgeInsets.fromLTRB(dp3, dp1, dp3, dp1),
                                      decoration: BoxDecoration(
                                        border: Border.all(
                                          color: const Color(0xFFFF1F14),
                                          width: 1,
                                        ),
                                        borderRadius: BorderRadius.circular(dp2),
                                      ),
                                      child: const Text(
                                        '锁单价',
                                        style: TextStyle(
                                          color: Color(0xFFFF5142),
                                          fontSize: 14,
                                        ),
                                      ),
                                    ),
                                ],
                              )),
                            ],
                          ),
                        ),

                        // 金额区域
                        Padding(
                          padding: EdgeInsets.only(left: dp15, bottom: dp12),
                          child: RichText(
                            text: TextSpan(
                              children: [
                                TextSpan(
                                  text: data.couponUnit != '折' ? '￥' : '',
                                  style: TextStyle(fontSize: 12, color: Color(0xFF13345F), fontWeight: FontWeight.bold),
                                ),
                                TextSpan(
                                  text: '${data.couponDenomination}',
                                  style: TextStyle(fontSize: 30, color: Color(0xFF13345F), fontWeight: FontWeight.bold),
                                ),
                                TextSpan(
                                  text: data.showUnit,
                                  style: const TextStyle(fontSize: 12, color: Color(0xFF13345F), fontWeight: FontWeight.normal),
                                ),
                              ],
                            ),
                          ),
                        ),
                      ],
                    ),
                  ),

                  // 有效期
                  Padding(
                    padding: EdgeInsets.only(left: 15, right: 15, bottom: 8),
                    child: Row(
                      children: [
                        Text(
                          '有效期: ${data.validDate}',
                          style: const TextStyle(
                            color: Color(0xFF666666),
                            fontSize: 12,
                          ),
                        ),
                        Spacer(),
                        Text(
                          data.thresholdDesc ?? '',
                          style: const TextStyle(
                            color: Color(0xFF666666),
                            fontSize: 12,
                          ),
                        ),
                      ],
                    ),
                  ),

                  // 虚线分隔线
                  const Padding(
                    padding: EdgeInsets.fromLTRB(15, 0, 10, 0),
                    child: HorizontalDashedLine(
                      width: double.infinity,
                      color: Colors.blue,
                    ),
                  ),

                  // 使用规则行
                  Padding(
                    padding: const EdgeInsets.only(top: 5, left: 15, right: 8),
                    child: Row(
                      children: [
                        const Text(
                          '使用规则',
                          style: TextStyle(
                            color: Color(0xFF666666),
                            fontSize: 12,
                          ),
                        ),
                        const Icon(Icons.keyboard_arrow_right, size: 16.0, color: Colors.grey),
                        const Spacer(),
                        Obx(() => SizedBox(
                              width: (mProvider.selectedCoupon.contains(data) && data.couponNum! > 1) ? 110 : 0,
                              child: Align(
                                  child: CartItemQuantityPicker(
                                initialQuantity: data.couponUseNum!,
                                maxQuantity: mProvider.getMaxCouponNum(data),
                                onQuantityChanged: (newQuantity) {
                                  // 使用 addPostFrameCallback 确保在当前构建阶段结束后更新状态
                                  WidgetsBinding.instance.addPostFrameCallback((_) {
                                    data.couponUseNum = newQuantity;
                                    print('新数量是：$newQuantity - ${data.couponUseNum} / ${mProvider.getMaxCouponNum(data)} / ${data.couponNum} / ${data.overlayLimit} / ${mProvider.couponLimit.value.couponOverlayLimit}');
                                    onTap?.call();
                                  });
                                },
                              ) // 不展示任何内容
                                  ),
                            )),
                      ],
                    ),
                  ),

                  // 使用规则详情
                  Padding(
                    padding: const EdgeInsets.fromLTRB(15, 5, 15, 12),
                    child: Text(
                      data.couponDesc ?? '',
                      style: const TextStyle(
                        color: Color(0xFF666666),
                        fontSize: 12,
                      ),
                      maxLines: 1,
                      overflow: TextOverflow.ellipsis,
                    ),
                  ),
                ],
              ),
            ),
          ),

          if (data.isOverlay)
            Container(
              height: dp8,
              margin: EdgeInsets.symmetric(horizontal: dp8),
              decoration: const BoxDecoration(
                color: Colours.white,
                borderRadius: BorderRadius.vertical(
                  bottom: Radius.circular(8),
                ),
                border: Border(
                  top: BorderSide.none,
                  left: BorderSide(color: Colours.c_F6E2C8, width: 1),
                  right: BorderSide(color: Colours.c_F6E2C8, width: 1),
                  bottom: BorderSide(color: Colours.c_F6E2C8, width: 1),
                ),
              ),
            ),
          if (data.isOverlay)
            Container(
              height: dp8,
              margin: EdgeInsets.symmetric(horizontal: dp16),
              decoration: BoxDecoration(
                color: Colours.white,
                borderRadius: BorderRadius.vertical(
                  bottom: Radius.circular(8),
                ),
                border: Border(
                  top: BorderSide.none,
                  left: BorderSide(color: Colours.c_F6E2C8, width: 1),
                  right: BorderSide(color: Colours.c_F6E2C8, width: 1),
                  bottom: BorderSide(color: Colours.c_F6E2C8, width: 1),
                ),
              ),
            ),
        ],
      ),
    );
  }
}
